package com.mjk.common.core.jdbc.datasource;

import com.mjk.common.core.jdbc.annotation.ArcSharding;
import com.mjk.common.core.jdbc.ibatis.typehandler.handler.StringHandler;
import com.mjk.common.core.jdbc.service.plus.constns.BeeTypeEntity;
import com.mjk.common.core.jdbc.service.plus.provider.BeeDataSourceType;
import com.mjk.common.core.jdbc.service.plus.provider.interfaces.IBeeTypeMapping;
import com.mjk.common.core.jdbc.util.DataSourceUtil;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.tools.springextend.BeeApplicationListener;
import com.mjk.common.util.ObjectUtils;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.type.JdbcType;
import org.springframework.context.EnvironmentAware;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public interface IDataSource extends EnvironmentAware, BeeApplicationListener {
	final String JIMU_DATASOURCE = "jimu";
	Map<String, DataSource> getAllDataSource();
	List<JdbcTemplate> getAllJdbcTemplate();
	
	JdbcTemplate getJdbcTemplate(ArcSharding arcSharding);
	
	DataSource getDataSource(ArcSharding arcSharding);

	default DataSource getJimuDataSource(Map<String, DataSource> dataSourceMap) {
		if (dataSourceMap.containsKey(JIMU_DATASOURCE)) {
			return dataSourceMap.get(JIMU_DATASOURCE);
		}
		return dataSourceMap.values().iterator().next();
	}
	/**
	 * 初始化jdbctype ,jdcbname,javatype 类型映射关系
	 */
//	default void  initJdbcMapping(String driver,DataSource dataSource){
//		IBeeTypeMapping mapping = BeeDataSourceType.getMapping(driver);
//		try(Connection connection = dataSource.getConnection();) {
//			//获取字段映射关系 每一个数据源保存一个数据映射
//			ResultSet set = connection.getMetaData().getTypeInfo();
//			while (set.next()) {
//				String typeName = set.getString("TYPE_NAME");
//				int sqlType = set.getInt("DATA_TYPE");
//				Set<BeeTypeEntity> entities = mapping.getMapping();
//				for(BeeTypeEntity entity : entities){
//					if(entity.getType().TYPE_CODE == sqlType){
//						JdbcType jdbcType = JdbcType.forCode(sqlType);
//						System.out.println(sqlType+"---"+typeName+"---"+jdbcType.name());
//					//	entity.setJdbcType(ObjectUtils.isEmpty(jdbcType.name())?typeName:jdbcType.name());
//						continue;
//					}
//				}
//			}
//		}catch (Exception ignored){
//			BizLogger.error(ignored);
//		}
//	}
}
